﻿using HardToolApp.Model;
using HardToolApp.Tool;
using OxyPlot;
using OxyPlot.Series;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HardToolApp.View
{
    /// <summary>
    /// ADCFullPage.xaml 的交互逻辑
    /// </summary>
    public partial class ADCFullPage:Window
    {
        ADCModel model;

        LineSeries lineAdd(string title,ADCModel.ADCChannel channel)
        {
            LineSeries chSeries = new LineSeries { Title = title, MarkerType = OxyPlot.MarkerType.Square };
            for (int i = 0; i < channel.chList.Count; i++)
            {
                chSeries.Points.Add(new DataPoint(i, channel.chList[i]));
            }
            return chSeries;
        }
        public ADCFullPage(ADCModel model)
        {
            InitializeComponent();
            this.model = model;
            PlotModel Plot = new PlotModel();

            Plot.Series.Add(lineAdd("CH1", model.ch1));
            Plot.Series.Add(lineAdd("CH2", model.ch2));
            Plot.Series.Add(lineAdd("CH3", model.ch3));
            Plot.Series.Add(lineAdd("CH4", model.ch4));
            chart.Model = Plot;
            Plot.InvalidatePlot(true);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            string path = Common.OpenSaveDialog("数据(.csv)|*.csv");
            if(path != null)
            {
                StringBuilder sb = new StringBuilder();
                
                for (int i = 0; i < model.ch1.chList.Count; i++)
                {
                    sb.Append(model.ch1.chList[i].ToString());
                    sb.Append(',');
                    sb.Append(model.ch2.chList[i].ToString());
                    sb.Append(',');
                    sb.Append(model.ch3.chList[i].ToString());
                    sb.Append(',');
                    sb.Append(model.ch4.chList[i].ToString());
                    sb.Append("\r\n");
                }
                File.WriteAllText(path, sb.ToString());
            }
        }
    }
}
